package day05.Test3;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

class Person {
    String name;
    int age;
    String sex;

    public Person() {
    }

    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
}

public class SimpleMapper {
    public static String toString(Object obj) throws IllegalAccessException {
        Class<?> clazz = obj.getClass();//也可以Class.forName(obj.getClass().getName())
        StringBuilder sb = new StringBuilder();
        sb.append(clazz.getName()).append("\n");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            sb.append(field.getName()).append("=").append(field.get(obj)).append("\n");
        }
        return sb.toString();
    }

    public static Object fromString(String string) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        String[] str = string.split("\n");
        if (str.length == 0) {
            System.out.println("nonono");
        }
        Class<?> clazz = Class.forName(str[0]);
        Object obj = clazz.newInstance();
        for (int i = 0; i < str.length - 1; i++) {
            Field field = clazz.getDeclaredField(EqualssignFirstString(str[i + 1]));
            field.setAccessible(true);
            if (field.getType() == int.class) {
                field.set(obj, Integer.parseInt(EqualssignLastString(str[i + 1])));
            } else if (field.getType() == String.class) {
                field.set(obj, EqualssignLastString(str[i + 1]));
            }
        }

        return obj;

    }

    private static String EqualssignFirstString(String str) {
        return str.split("=")[0];
    }

    private static String EqualssignLastString(String str) {
        return str.split("=")[1];
    }

}
/* *
class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class SetValueUsingReflection {
    public static void main(String[] args) throws Exception {
        Person person = new Person();
        Class<?> clazz = person.getClass();

        // 设置 name 属性值
        Field nameField = clazz.getDeclaredField("name");
        nameField.setAccessible(true);
        nameField.set(person, "John");

        // 设置 age 属性值
        Method setAgeMethod = clazz.getMethod("setAge", int.class);
        setAgeMethod.invoke(person, 30);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}
        // 获取要调用的方法
        Method method = testObject.getClass().getMethod("methodWithParameters", String.class, int.class);

        // 调用方法并传入参数
        method.invoke(testObject, "Hello", 123);
* */